package tako.wisers.com.android.apis.activity;

import tako.wisers.com.android.apis.R;
import tako.wisers.com.android.apis.adapter.ImageAdapter;
import tako.wisers.com.android.apis.adapter.TopButtonBarAdapter;
import tako.wisers.com.android.apis.event.Event;
import tako.wisers.com.android.apis.event.EventBus;
import tako.wisers.com.android.apis.handler.Handler;
import tako.wisers.com.android.apis.handler.ListSwitchHandler;
import tako.wisers.com.android.apis.handler.MapSwitchHandler;
import tako.wisers.com.android.apis.handler.SearchHandler;
import tako.wisers.com.android.apis.item.BaseItem;
import tako.wisers.com.android.apis.item.NewsItem;
import tako.wisers.com.android.apis.item.PanoramioItem;
import tako.wisers.com.android.apis.item.StationItem;
import tako.wisers.com.android.apis.listener.GConClick;
import tako.wisers.com.android.apis.listener.KeyListener;
import tako.wisers.com.android.apis.listener.SearchFocusChangeListenser;
import tako.wisers.com.android.apis.location.GPSManager;
import tako.wisers.com.android.apis.parser.xml.item.ButtonItem;
import tako.wisers.com.android.apis.util.BackGroundHelper;
import tako.wisers.com.android.apis.util.Configration;
import tako.wisers.com.android.apis.util.Global;
import tako.wisers.com.android.apis.view.DialogManager;
import tako.wisers.com.android.apis.view.MapViewControl;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class Tako extends MapActivity implements OnItemClickListener,
		OnClickListener {
	/** Called when the activity is first created. */
	private MapView mMapView;
	private ListView mlist;
	private EventBus mbus;
	private Configration conf;
	private final DialogManager mDialogManager = new DialogManager(this);
	private TopButtonBarAdapter mTopAdapter;

	private int bottomDisplay;
	private Dialog mProgressDialog;

	private static final int GPS_LISTENER = 1;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		conf = Configration.getInstance();

		mbus = EventBus.getInstance();
		FrameLayout frame = (FrameLayout) findViewById(R.id.frame);
		mMapView = new MapView(this, conf.getProperty(Global.CONF_APIKEY));
		mMapView.setOnLongClickListener(new GConClick());

		MapViewControl mapViewC = new MapViewControl(this, mMapView);
		mapViewC.settingMapView();

		frame.addView(mMapView, new FrameLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

		bottomDisplay = Global.MAP_DISPLAY;
		initCompantsEvent();
		initList();
		BackGroundHelper.settingBackGround(this, R.layout.main);
		System.gc();
	}

	private void initList() {
		mlist = new ListView(this);
		mlist.setAdapter(new ImageAdapter(this));
		mlist.setOnItemClickListener(this);
		mlist.setBackgroundDrawable(null);
	}

	private void initCompantsEvent() {
		intiButton(R.id.map, new MapSwitchHandler(this));
		intiButton(R.id.list, new ListSwitchHandler(this));
		mTopAdapter = new TopButtonBarAdapter(this);
		Gallery topbar = (Gallery) findViewById(R.id.top_button_bar);
		topbar.setAdapter(mTopAdapter);
		topbar.setOnItemClickListener(mTopAdapter);
		intiButton(R.id.search, new SearchHandler(this));

		mbus.signinEvent(R.id.list, new ListSwitchHandler(this),
				Event.EVENT_SWITCH);

		View searchText = findViewById(R.id.keyword);
		mbus.signinEvent(R.id.keyword, new SearchHandler(this),
				Event.EVENT_KEY_DOWN);
		searchText.setOnKeyListener(new KeyListener());
		searchText.setOnFocusChangeListener(new SearchFocusChangeListenser());
	}
	
	public ButtonItem getTopButtonItem(){
		return mTopAdapter.getCurrentTopButton();
	}

	private void intiButton(int id, Handler handler) {
		View view = findViewById(id);
		mbus.signinEvent(id, handler, Event.EVENT_CLICK);
		view.setOnClickListener(this);
	}

	public void switchDisplay(int type) {
		FrameLayout frame = (FrameLayout) findViewById(R.id.frame);
		frame.removeAllViews();
		switch (type) {
		case Global.IIID_DISPLAY:
			break;
		case Global.MAP_DISPLAY:
			frame.addView(mMapView);
			break;
		case Global.LIST_DISPLAY:
			frame.addView(mlist);
			break;
		default:

		}
	}

		
	public MapView getMapView() {
		return mMapView;
	}

	public ListView getList() {
		return mlist;
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	public void onItemClick(AdapterView<?> l, View v, int position, long id) {
		BaseItem item = (BaseItem) l.getItemAtPosition(position);
		try {
			Intent intent = new Intent();
			intent.removeExtra(Global.ITEM_KEY);
			intent.putExtra(Global.ITEM_KEY, item);
			if (item instanceof NewsItem) {
				intent.setClassName(this, Global.NEWS_INTENT_ACTIVITY);
			} else if (item instanceof StationItem) {
				intent.setClassName(this, item.getActivity());
			} else if (item instanceof PanoramioItem) {
				intent.setClassName(this, Global.PHOTO_INTENT_ACTIVITY);
			}
			startActivity(intent);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.gc();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			dialogShow(Global.EXIT_DIALOG);
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	public int getBottomDisplay() {
		return bottomDisplay;
	}

	public void setBottomDisplay(int bottomDisplay) {
		this.bottomDisplay = bottomDisplay;
	}

	public void dialogShow(int id) {
		mProgressDialog = mDialogManager.build(id);
		mProgressDialog.show();
	}

	@Override
	public void onClick(final View v) {
		dialogShow(Global.READ_DIALOG);
		mbus.executeEvent(new Event(v.getId(), Event.EVENT_CLICK, true));
	}

	public void dismissDialog() {
		mProgressDialog.dismiss();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, GPS_LISTENER, 0, R.string.location_gps).setIcon(
				R.drawable.ic_menu_radar);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case GPS_LISTENER:
			GPSManager gm = GPSManager.getInstance();
			if (!gm.isListener()) {
				item.setTitle(R.string.location_no_gps);
				gm.setGPSListener(true);
			} else {
				item.setTitle(R.string.location_gps);
				gm.setGPSListener(false);
			}
		}
		return true;
	}
}